package com.fhz.day005

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration

/**
 * 时间:2021/10/18
 * @author msfeng
 * 简述: TODO
 */
@Database(version =5,entities = [User::class,Student::class])
abstract class SqlData : RoomDatabase(){

    abstract fun getUserDao(): UserDao
    abstract fun getStudentDao(): StudentDao

    companion object{
        var sqlData:SqlData? = null

        @Synchronized
        fun getSqlData(context: Context) : SqlData {
            sqlData?.let {
                return it
            }

            return Room.databaseBuilder(context,SqlData::class.java,"user.db")
                .allowMainThreadQueries()
//                .fallbackToDestructiveMigration()
                .addMigrations(MyMigration(4,5))
                .build().apply {
                    sqlData = this
                }
        }

    }

}